// 加载配置文件
const config = require( '../config.js' );
const db = wx.cloud.database();
function get(url,id,fun,errfun,model){
    db.collection(url).doc(id).get({
        success: res => {
            fun(res);
        },
        fail: err => {
            errfun(err);
        }
    })
}
function count(url,data,fun,errfun,model){
    db.collection(url).where(data).count({
        success: res => {
            fun(res);
        },
        fail: err => {
            errfun(err);
        }
    })
}
function query(url,data,page,pageSize,fun,errfun){
    db.collection(url).where(data)
    .skip(page*pageSize) // 跳过结果集中的前 10 条，从第 11 条开始返回
    .limit(pageSize) // 限制返回数量为 10 条
    .get()
    .then(res => {
        fun(res.data);
    })
    .catch(err => {
        errfun(err);
    })
}
function update(url,id,newdata,fun,errfun,model){
    db.collection(url).doc(id).update({
        data: newdata,
        success: res => {
            fun(res);
        },
        fail: err => {
            errfun(err);
        }
    })
}
function find(url,data,fun,errfun,model){
    db.collection(url).where(data)
      .get({
        success: res => {
            console.log(res);
            fun(res);
        },
        fail: err => {
            errfun(err);
        }
    })
}
function remove(url,id,fun,errfun,model){
    db.collection(url).doc(id).remove({
        success: res => {
            fun(res);
        },
        fail: err => {
            errfun(err);
        }
    })
}
function add(url,data={},fun,errfun,model){
    db.collection(url).add({
        data: data,
        success: res => {
          fun(res);
        },
        fail: err => {
          errfun(err);
        }
    })
}

function AJAX(url, data = {}, fn,errfun, method = "GET",type) {
    wx.request({
      url: config.service.requestUrl + url,
      method: method ? method : 'POST',
      data: data,
      header: { 'content-type': type ? 'application/json' : 'application/x-www-form-urlencoded', accept: 'application/json' },
      success: function (res) {
        var datas = res.data;
        if (datas.success) {
          var data = datas.data;
          if (data && !(data instanceof Object)) {
            data = JSON.parse(data);
          }
          fn(data);
        } else {
            // wx.showModal({
            //   title: '提示',
            //   content: datas.msg,
            //   showCancel: false,
            //   success: function (res) {}
            // })
            errfun(datas);
        }
      }
    });
}
function AJAX2(url, data = {}, fn, errfun, method = "GET", type) {
  wx.request({
    url: config.service.requestUrl + url,
    method: method ? method : 'POST',
    data: data,
    header: { 'content-type': type ? 'application/json' : 'application/x-www-form-urlencoded', accept: 'application/json' },
    success: function (res) {
      fn(res.data);
    }
  });
}
module.exports = {
    get: get, //更具id获取单个数据
    query:query, //依据查询条件获取分页数据
    update:update, //更新数据
    find: find, //根据查询条件获取数据
    add:add, //添加数据
    count:count, //获取数据数量
    remove:remove, //删除数据
    AJAX: AJAX,//远程请求
    AJAX2: AJAX2,//远程请求
    imgurl: config.service.imgurl
}